package com.logic.landseaserver.common.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.logic.landseaserver.common.token.impl.WechatBroAccessTokenSerivce;

/**
 * @Author: Wells.Wei
 * @Date: 2017/5/10
 * @Description:
 */
public class AddressUtils {

    private static final Logger LOG = LoggerFactory.getLogger(AddressUtils.class);
    
    public static String getAddressByIP(String ip){

        URL url = null;
        BufferedReader reader = null;
        try {
            url = new URL("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&ip=" + ip);
            URLConnection conn = url.openConnection();
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
            String line = null;
            StringBuffer result = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            return result.toString();
        } catch (Exception e) {
            LOG.error("getAddressByIP error.", e);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                LOG.error("getAddressByIP error.", e);
            }
        }
        return "";
    }
}
